package com.tongqin.distributed;

import org.apache.activemq.ActiveMQConnectionFactory;

import javax.jms.*;

/**
 * Hello world!
 */
public class App {


    public static void main(String[] args) {

        ConnectionFactory connectionFactory = new ActiveMQConnectionFactory("tcp://172.16.233.128:61616");
        Connection connection = null;
        Session session = null;
        try {
            connection = connectionFactory.createConnection();
//            connection.setClientID();
            connection.start();
            session = connection.createSession(true, Session.AUTO_ACKNOWLEDGE);
            Destination destination = session.createQueue("myqueue");
            MessageConsumer consumer = session.createConsumer(destination);
            //如果消息队列中没有消息，会处于阻塞状态
//            TextMessage receive = (TextMessage) consumer.receive();
//            if(receive instanceof TextMessage){
//                TextMessage textMessage = (TextMessage) receive;

//                System.out.println("信息的内容"+ receive.getText());
//            }


            while (true) {
                consumer.setMessageListener(new MessageListener() {
                    @Override
                    public void onMessage(Message message) {
                        TextMessage textMessage = (TextMessage) message;

                        try {
                            System.out.println("监听的方式：" + textMessage.getText());
                        } catch (JMSException e) {
                            e.printStackTrace();
                        }
                    }
                });
                session.commit();
            }

//

        } catch (JMSException e) {
            e.printStackTrace();
        } finally {
            try {
                if (connection != null) {
                    connection.close();
                }
                if (session != null) {
                    session.close();
                }
            } catch (JMSException e) {
                e.printStackTrace();
            }

        }
        System.out.println("哪里有问题");

    }
}
